<?php

namespace BoxAdmin\Controller;

use Think\Controller;

class IntegralController extends CommonController
{

    /**
     * 商家积分管理
     */
    public function index()
    {
        $this->memuNav = 'index';
        $where = 'store_type=2';
        if ($this->store_type == 2) $where .= " and store_id = $this->store_id";
        $kw = I('kw', '', 'trim');
        if ($kw) $where .= " and store_name like '%$kw%'";
        $map = array(
            'where' => $where,
            'order' => 'store_id desc'
        );
        $this->data = $this->lists('store', $map);
        $this->display();
    }

    /**
     * 充值记录
     * @return [type] [description]
     */
    public function visit()
    {
        $this->memuNav = 'visit';
        $this->source_type = $source_type = I('source_type', 4, 'intval');
        $where = "V.type='cz' and V.source_type=" . $source_type;
        $store_id = I('store_id', '', 'intval');
        $kw = I('kw', '', 'trim');
        if ($kw) $where .= " and S.store_name like '%$kw%'";
        if ($store_id) $where .= " and V.store_id=$store_id";
        if (!$store_id) {
            if ($this->store_type == 2) $where .= " and V.store_id = $this->store_id";
        }
        $kw = I('kw', '', 'trim');
        if ($kw) $where .= " and S.store_name like '%$kw%'";
        $map = array(
            'where' => $where,
            'order' => 'V.id desc',
            'alias' => 'V',
            'field' => 'S.store_name,V.*',
            'join' => array(
                'left join ps_store S on S.store_id=V.store_id'
            ),
        );
        $this->data = $this->lists('member_integral_visit', $map);
        $this->display();
    }

    /**
     * 商家去充值
     */
    public function gocz()
    {
        $this->memuNav = 'index';
        $payment = BoxModel('payment')->where("enabled=1 and type = 'web'")->select();
        $this->payment = $payment;
        $this->display();
    }

    public function toczdo()
    {
        if (IS_POST) {
            $num = I('num', 1);
            $payment_id = I('payment_id');
            if (!$payment_id) exit(json_encode(array('error' => 1, 'msg' => '请选择支付方式')));
            $add = array(
                'order_sn' => createOrderNo(),
                'add_time' => time(),
                'payment_id' => $payment_id,
                'order_amount' => $num,
                'goods_amount' => $num,
                'type' => I('type'),
                'store_id' => $this->store_id
            );

            $order_id = BoxModel('order_public')->add($add);
            if (!$order_id) {
                exit(json_encode(array('error' => 1, 'msg' => '订单创建失败')));
            } else {
                $order_id = $order_id . '|order_public';
                $weburl = "http://" . $_SERVER['HTTP_HOST'];
                if (C('APP_SUB_DOMAIN_DEPLOY') == 1) {
                    foreach (C('APP_SUB_DOMAIN_RULES') as $key => $value) {
                        if ($key != '*') {
                            $key = str_replace('*', '', $key);
                            $weburl = str_replace($key, '', $weburl);
                        }
                    }
                }
                $url = $weburl . '/Pay/index?order_id=' . $order_id;
                exit(json_encode(array('error' => 0, 'msg' => 'ok', 'url' => $url)));
            }
        }
    }

    public function integralSet()
    {
        $this->memuNav = 'integralSet';
        $where = 'store_id=' . $this->store_id;
        $order = 'id desc';
        $model = BoxModel('member_integral');
        $this->setPager($model, 8, $where);
        $this->data = $model->where($where)->order($order)->limit($this->limit)->select();
        /*签到规则*/
        $qiandao = BoxModel('qiandao')->where(array('store_id' => $this->store_id))->find();
        if ($qiandao) {
            $this->qd = json_decode($qiandao['integral'], true);
        }
        $this->display();
    }

    public function integral_add()
    {
        $this->memuNav = 'integral';
        if ($id = I('id')) {
            $this->data = BoxModel('member_integral')->find($id);
        }
        $this->display();
    }

    public function integral_add_do()
    {
        if (IS_POST) {
            $data = array(
                'num' => I('num'),
            );
            $model = BoxModel('member_integral');
            if ($id = I('id')) {
                $model->where(array('id' => $id))->save($data);
            } else {
                $data['store_id'] = $this->store_id;
                $data['name'] = I('name');
                $data['keyword'] = I('keyword');
                $model->add($data);
            }
            $this->redirect('integralSet');
        }
    }

    public function integral_delete()
    {
        if ($id = I('id')) {
            BoxModel('member_integral')->delete($id);
            $this->redirect('integralSet');
        }
    }

    public function qiandao_add()
    {
        if (IS_POST) {
            $data = I('post.');
            $arr = array();
            for ($i = 0; $i < count($data['datenum']); $i++) {
                $arr[$i]['datenum'] = $data['datenum'][$i];
                $arr[$i]['integral'] = $data['integral'][$i];
            }
            $add['integral'] = json_encode($arr);
            $model = BoxModel('qiandao');
            $where = array('store_id' => $this->store_id);
            $find = $model->where($where)->find();
            if ($find) {
                $model->where($where)->save($add);
            } else {
                $add['store_id'] = $this->store_id;
                $model->add($add);
            }
            $this->redirect('integralSet');

        }
    }

    public function save($id, $status)
    {
        BoxModel('member_integral')->where('id=' . $id)->setField('status', $status);
        $this->redirect('integralSet');
    }
}